<template>
  <div class="common-container">
    <div class="common-content">
      <div class="person-card">
        <el-form label-width="80px" size="small" class="person-form">
          <div class="avatar-uploader">
            <el-upload
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
            >
              <img v-if="form.avatarImg" :src="form.avatarImg" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>

          <el-form-item label="用户名">
            <el-input v-model="form.userName" disabled autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save">确 定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      uploadUrl: 'http://localhost:7777/file/upload'
    }
  },
  created() {
    this.getUser().then(res => {
      this.form = res
    }).catch(error => {
      console.error('获取用户信息失败:', error);
      this.$message.error('获取用户信息失败');
    });
  },
  methods: {
    async getUser() {
      try {
        const response = await this.request.get(`/user/userName/${this.user.userName}`);
        return response.data;
      } catch (error) {
        console.error('请求用户信息失败:', error);
        throw error;
      }
    },
    save() {
      this.request.post("/user", this.form).then(res => {
        if (res.code === 200) {
          this.$message.success("保存成功");

          // 触发父级更新User的方法
          this.$emit("refreshUser");

          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            const userInfo = JSON.parse(localStorage.getItem("user"));
            res.token = userInfo.token;
            localStorage.setItem("user", JSON.stringify(res));
          }).catch(error => {
            console.error('更新本地用户信息失败:', error);
          });

        } else {
          this.$message.error("保存失败");
        }
      }).catch(error => {
        console.error('保存用户信息失败:', error);
        this.$message.error('保存用户信息失败');
      });
    },
    handleAvatarSuccess(res) {
      this.form.avatarImg = res;
    }
  }
}
</script>

<style scoped>
.common-container {
  padding: 20px;
  background-color: #f5f7fa;
}

.common-content {
  margin-bottom: 20px;
}

.person-card {
  width: 500px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.person-form {
  width: 100%;
}

.avatar-uploader {
  text-align: center;
  padding-bottom: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin: 0 auto;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}

.avatar {
  width: 138px;
  height: 138px;
  display: block;
  border-radius: 6px;
}

.el-form-item {
  margin-bottom: 18px;
}

.el-form-item:last-child {
  margin-bottom: 0;
  text-align: center;
}

.el-button {
  padding: 8px 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .person-card {
    width: 100%;
    max-width: 500px;
    padding: 15px;
  }
  
  .common-container {
    padding: 10px;
  }
}
</style>
